#include "passwd.h"
#include "ui_passwd.h"
#include "messageBox/messagebox.h"


Passwd::Passwd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Passwd)
{
    ui->setupUi(this);
    setWindowTitle(QString("修改密码"));
}

Passwd::~Passwd()
{
    delete ui;
}

void Passwd::on_pushButton_ok_clicked()
{
    QString oldPasswd = ui->lineEdit_old_passwd->text();
    QString newPasswd = ui->lineEdit_new_passwd->text();
    QString okPasswd = ui->lineEdit_passwd_confirm->text();

    if(oldPasswd.isEmpty() || newPasswd.isEmpty() || okPasswd.isEmpty()){
        MessageBox *box = new MessageBox(this,
                             MessageBox::Error,
                             "密码修改",
                             "密码输入不完整，请重新输入!",
                             QMessageBox::Ok, QMessageBox::Ok);
        box->exec();
        if(box){
            box->deleteLater();
        }
        return;
    }

    if(oldPasswd != dLogin->getLoginUser()->getPasswd()){
        MessageBox *box = new MessageBox(this,
                             MessageBox::Error,
                             "密码错误",
                             "旧密码输入错误，请重新输入!",
                             QMessageBox::Ok, QMessageBox::Ok);
        box->exec();
        if(box){
            box->deleteLater();
        }
        return;
    }

    if(oldPasswd == newPasswd){
        MessageBox *box = new MessageBox(this,
                             MessageBox::Warning,
                             "密码未修改",
                             "旧密码与新密码一致!",
                             QMessageBox::Ok, QMessageBox::Ok);
        box->exec();
        if(box){
            box->deleteLater();
        }
        return;
    }

    if(newPasswd != okPasswd){
        MessageBox *box = new MessageBox(this,
                             MessageBox::Warning,
                             "密码输入有误",
                             "两次输入的密码不一致，请核对后重新输入!",
                             QMessageBox::Ok, QMessageBox::Ok);
        box->exec();
        if(box){
            box->deleteLater();
        }
        return;
    }

    QString userName = dLogin->getLoginUser()->getName();
    if(dLogin->getLoginDB()->updateUserInfo(userName,newPasswd)){
        MessageBox *box = new MessageBox(this,
                             MessageBox::Messsage,
                             "提示",
                             "密码修改成功！!",
                             QMessageBox::Ok, QMessageBox::Ok);
        if(box->exec() == QMessageBox::Ok)
        {
            if(box){
                box->deleteLater();
            }
            this->close();
        }
        return;
    }

    MessageBox *box = new MessageBox(this,
                         MessageBox::Error,
                         "提示",
                         "密码修改失败！!",
                         QMessageBox::Ok, QMessageBox::Ok);
    if(box){
        box->deleteLater();
    }
    this->close();
}

void Passwd::on_pushButton_cancel_clicked()
{
    this->close();
}

void Passwd::setDLogin(Dialog_Login *value)
{
    dLogin = value;
}
